import router from "@/router";
import axios from "axios";
import { Toast } from 'vant';
import 'vant/es/toast/style';

// 'http://120.79.93.103:7001'
axios.defaults.baseURL = 'http://120.79.93.103:7001'                  
axios.defaults.withCredentials = true   //开启axios携带凭证（token）的能力
axios.defaults.headers['X-Request-With'] = 'XMLHttpRequest'
axios.defaults.headers['Authorization'] = `${sessionStorage.getItem('token') || null}`
axios.defaults.headers.post['Content-Type'] = 'application/json'


axios.interceptors.response.use(res=>{       //响应拦截
    if(typeof res.data !== 'object'){   //服务端报错
        console.log(res.data);
        Toast.fail('服务端异常')
        return Promise.reject(res)
    }

    if(res.data.code !== 200){
        if(res.data.msg) Toast.fail(res.data.msg)
        if(res.data.code == 401 ){    //未授权
            console.log('未授权');
            router.push('/login')
        }
        return Promise.reject(res.data)
    }

    return res.data
})

export default axios 